//
//  NocticeSetViewController.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/26.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "NocticeSetViewController.h"
#import "XGPush.h"

@interface NocticeSetViewController ()
@property (weak, nonatomic) IBOutlet UIButton *starPush;
@property (weak, nonatomic) IBOutlet UILabel *pushTitile;

@property (weak, nonatomic) IBOutlet UISwitch *zdSwitch;
@end

@implementation NocticeSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if([[userDefaults objectForKey:@"zhendongOn"] isEqualToString:@"1"]){
        _zdSwitch.on = YES;
    }else{
        _zdSwitch.on = NO;
    }
   [self setNavTitle:@"消息与声音设置" titleColor:[UIColor blackColor] font:font17];
    _starPush.layer.cornerRadius = _starPush.height/2;
    _starPush.layer.masksToBounds = YES;
    [_starPush addTarget:self action:@selector(reGetToken) forControlEvents:UIControlEventTouchUpInside];
    if([userDefaults objectForKey:@"deviceToken"]){
        _starPush.hidden = NO;
       
    }else{
        _starPush.hidden = YES;
      
    }
    
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIRemoteNotificationTypeNone) {
        _pushTitile.text = @"未开启";
    }else{
        _pushTitile.text = @"已开启";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)switchAction:(id)sender {
    UISwitch *switchs = sender;
    if(switchs.on){
        [userDefaults setObject:@"1" forKey:@"zhendongOn"];
    }else{
        [userDefaults setObject:@"0" forKey:@"zhendongOn"];
    }
}

///ShopApi/Login/reGetToken
- (void)reGetToken{
    
    NSData *deviceToken = [userDefaults objectForKey:@"deviceToken"];
    
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"YYYY/MM/dd/hh/mm/ss/SS"];//设定时间格式,这里可以设置成自己需要的格式
    NSString *dateString = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
    NSString *accout = [NSString stringWithFormat:@"honglabaTestsss_%@",dateString];
    
    
    NSString *deviceTokenStr = [XGPush registerDevice:deviceToken account:accout successCallback:^{
        NSLog(@"[XGDemo] register push success");
    } errorCallback:^{
        NSLog(@"[XGDemo] register push error");
       
    }];
    
    if(deviceTokenStr.length<1){
        [AlertUtils showToastWithMessage:@"设备标识获取失败" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        return;
    }
    
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Login/reGetToken" parameter:@{@"deviceToken":deviceTokenStr} successBlock:^(PPHttpRequest *request, id responseObj) {
        [AlertUtils showToastWithMessage:@"消息开启成功" inView:self.view hideAfterDelay:2.0 beIndeter:NO];
        [userDefaults removeObjectForKey:@"deviceToken"];
        _starPush.hidden = YES;
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
    
}

/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
